define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'hospital/hospital/index' + location.search,
                    add_url: 'hospital/hospital/add',
                    edit_url: 'hospital/hospital/edit',
                    del_url: 'hospital/hospital/del',
                    multi_url: 'hospital/hospital/multi',
                    import_url: 'hospital/hospital/import',
                    table: 'hospital',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'id', title: __('Id'),operate: false},
                        {field: 'title', title: __('Title'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'lat', title: __('Lat'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'lng', title: __('Lng'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'address', title: __('Address'), operate: false, table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'admin_id', title: __('Admin_id'),operate: false,formatter:function(value,row){
                            return row.admin?.username;
                        }},
                        {field: 'user_id', title: __('User_id'),formatter:function(value,row){
                            return `<div>姓名:${row?.user?.nickname}</div><div>手机:${row?.user?.mobile}</div>`;
                        }},
                        // {field: 'goods_ids', title: __('Goods_ids'), operate: false, table: table, class: 'autocontent', formatter: function(value,row){
                        //     return (row.goods_ids? row.goods_ids.split(',').length : 0)+'个';
                        // }},
                        {field: 'status', title: __('Status'), searchList: {"20":'已审核',"10":"待审核"}, formatter: function(value,row,index){
                            return Table.api.formatter.status.call(this,row.status,row,index)
                        }},
                        {field: 'discount', title: __('Discount'),operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,buttons:[
                            {
                                name: '项目折扣',
                                title: '项目折扣',
                                classname: 'btn btn-xs btn-success btn-dialog',
                                icon: 'fa fa-bookmark',
                                url: 'hospital/hospital_discount/index?id={ids}',
                                extend: 'data-area=\'["90%","90%"]\'',
                                visible: function (row) {
                                    return true;
                                }

                            }
                        ], formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        salehospital: function () {
            console.log(location.search);
            
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'hospital/hospital/salehospital' + location.search,
                    table: 'hospital',
                }
            });

            var table = $("#tableSale");
            console.log(table);
            

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'lat', title: __('Lat'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'lng', title: __('Lng'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'address', title: __('Address'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'admin_id', title: __('Admin_id')},
                        {field: 'user_id', title: __('User_id'),operate:false,formatter:function(value,row){
                            return `<div>姓名:${row?.user?.nickname}</div><div>手机:${row?.user?.mobile}</div>`;
                        }},
                        {field: 'goods_ids', title: __('Goods_ids'), operate:false, table: table, class: 'autocontent', formatter: function(value,row){
                            return (row.goods_ids? row.goods_ids.split(',').length : 0)+'个'; 
                        }},
                        {field: 'discount', title: __('Discount'),operate:false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
